﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Utils;
using BitPortal.Business;
using BitPortal.Models;
namespace BitPortal.Admin.Page
{
    public partial class ModuleInPage : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException(role: "PageView", useExceptionHandler: true);
            PermissionBLL.CheckRoleThrowException(role: "PageEdit", useExceptionHandler: true);
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            Models.PortalPage page = Business.PageBLL.GetByID(Request.QueryString["PageID"].ToInt32TryParse());
            if (page != null)
            {
                this.Store1.DataSource = page.GetModule();
                this.Store1.DataBind();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                sm.ClearSelections();
            }
        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            BindData();
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            wAddModule.AutoLoad.Url = "/Admin/Module/PageModule/PageModuleAddEdit.aspx?PageID=" + Request.QueryString["PageID"] + "&ReloadID=" + GridPanel1.ClientID;
            wAddModule.LoadContent();
            wAddModule.Show();
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                wAddModule.AutoLoad.Url = "/Admin/Module/PageModule/PageModuleAddEdit.aspx?ModuleID=" + sm.SelectedRows[0].RecordID + "&PageID=" + Request.QueryString["PageID"] + "&ReloadID=" + GridPanel1.ClientID;
                wAddModule.LoadContent();
                wAddModule.Show();
            }
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("PageEdit");
                int record = 0;
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (BitPortal.Business.ModuleBLL.DeleteModuleByID(Convert.ToInt32(row.RecordID)) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " Module thành công!");
                    BindData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

    }
}